﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using OgilvyOne.Model;
using OgilvyOne.DALFactory;
using OgilvyOne.IDAL;
using System.Xml;
using OgilvyOne.Utility;

namespace OgilvyOne.BLL
{
    public class DictionaryManageBLL
    {
        public DataSet GetDictionary(string strWhere)
        {

            return DictionaryManageFactory.create().GetDictionary(strWhere);
            
        }

        private DictionaryManageIDAL Dictionary = DictionaryManageFactory.create();

        public int updateDictionary(DictionaryManageModel updDic)
        {
            return DictionaryManageFactory.create().updateDictionary(updDic);
        }
        public DictionaryManageModel selectDictionary(int cid)
        {
            return DictionaryManageFactory.create().selectDictionary(cid);
        }
        public int AddDictionary(DictionaryManageModel getDic)
        {
            return Dictionary.AddDictionary(getDic);
        }
        public bool deleteDictionary(int CID)
        {
            return Dictionary.deleteDictionary(CID);
        }
        public bool deleteDictionary(string CID)
        {
            return Dictionary.deleteDictionary(CID);
        }
        /// <summary>
        /// 获取子字典列表
        /// </summary>
        /// <param name="upid"></param>
        /// <returns></returns>
        public IList GetDictionary(int upid)
        {
            return Dictionary.GetDictionary(upid);
        }
        /// <summary>
        /// 查询是否有子栏目
        /// </summary>
        public int selectSon(int cid)
        {
            return DictionaryManageFactory.create().selectSon(cid);
        }

        public void saveXml(string upid)
        {
            DataSet ds = GetDictionary(" upid = '" + upid + "' and availabilityflag=0");
            XmlDocument doc = new XmlDocument();
            XmlDeclaration decl = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
            doc.AppendChild(decl);
            XmlNode result = doc.CreateElement("result");

            if (upid == "11")
            {
                XmlNode list = doc.CreateElement("list");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    XmlNode model = doc.CreateElement("model");

                    XmlNode id = doc.CreateElement("id");
                    id.InnerText = ds.Tables[0].Rows[i]["CID"].ToString();
                    model.AppendChild(id);

                    XmlNode name = doc.CreateElement("name");
                    name.InnerText = ds.Tables[0].Rows[i]["cnname"].ToString();
                    model.AppendChild(name);

                    XmlNode img = doc.CreateElement("img");
                    img.InnerText = ConConstants.UPLOADFILES_IMAGE_PATH + ds.Tables[0].Rows[i]["standby2"];
                    model.AppendChild(img);

                    list.AppendChild(model);
                }

                result.AppendChild(list);

                doc.AppendChild(result);
                string fn = "model.xml";
                FileUtil.SaveHtmlFile(System.Web.HttpContext.Current.Server.MapPath("~/testdrive/xml/") + fn, doc.OuterXml);
            }
        }
    }
}
